package com.adinnet.admin.peaseRule.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDateTime;

import javax.servlet.http.HttpServletRequest;

import com.adinnet.admin.common.service.CommonService;
import com.adinnet.admin.peaseRule.mapper.PeaseRuleMapper;
import com.adinnet.admin.peaseRule.model.PeaseRule;
import com.adinnet.admin.peaseRule.service.PeaseRuleService;
import com.adinnet.core.BaseController;
import com.adinnet.core.JsonResult;
import com.adinnet.core.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;

/**
 * <p>
 * 金豆规则 前端控制器
 * </p>
 *
 * @author dmm
 * @since 2020-12-15
 */
@Controller
@RequestMapping("admin/peaseRule")
public class PeaseRuleController extends BaseController {

    @Autowired
    protected HttpServletRequest request;
    
    @Autowired
    PeaseRuleMapper peaseRuleMapper;
    @Autowired
    PeaseRuleService peaseRuleService;
    @Autowired
    private CommonService commonService;


    /**
     * @Description TODO
     * @author dmm 
     * @param model
     * @createTime: 2020/12/15 10:51
    */
    @GetMapping
    public String index(Model model){
        return "peaseRule/peaseRule.html";
    }
    
    /**
     * 增删改查
     * @author dmm 
     * @param entity
     * @createTime: 2020/12/15 11:22
    */
    @Transactional(rollbackFor = Exception.class)
    @ResponseBody
    @RequestMapping("/crud")
    public JsonResult crud(String flag,PeaseRule entity){
        // if("del".equals(flag))peaseRuleMapper.deleteById(entity.getId()); 
        if("edit".equals(flag))peaseRuleMapper.updateById(entity); 
        if("add".equals(flag))peaseRuleMapper.insert(entity);
        if("edit".equals(flag)){
            // 分享类  修改一个 全部受影响
            if(entity.getType().intValue() == 4 ){
                UpdateWrapper<PeaseRule> uw = new UpdateWrapper<PeaseRule>();
                uw.set("pease_num",entity.getPeaseNum()).eq("type",4);
                peaseRuleService.update(uw);
            }
            commonService.saveOperation(request,"金豆规则-修改-"+(StringUtils.isNotBlank(entity.getMessage())?"行为事项":"金豆"));
        }
        return ok("操作成功");
    }

    /**
     * @Description TODO
     * @author dmm 
     * @param 参数
     * @createTime: 2020/12/15 11:23
    */
    @ResponseBody
    @RequestMapping("/list")
    public PageResult<PeaseRule> list(Integer page, Integer limit) {
        if (page == null || limit == null) { page = 1; limit = 10; }
        IPage<PeaseRule> iPage = peaseRuleMapper.selectPage(new Page<>(page, limit), new QueryWrapper<PeaseRule>().eq("deleted",0));
        return new PageResult<>(iPage.getRecords(), iPage.getTotal());
    }
}
